package cn.kgc.jdbc;

import cn.kgc.entity.Usr;
import cn.kgc.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @author mosin
 * date 2022/2/11
 * @version 1.0
 *模拟用户的登录注册
 */
public class JDBCDemo01 {

    public static void main(String[] args) {

       // register();
        login();
    }
    //用户注册
    public static void register(){
        Scanner scanner = new Scanner(System.in);
        //获取用户名
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        //获取密码
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        //获取用户的生日
        System.out.println("请输入生日(1990-2-3)");
        String birth = scanner.nextLine();
        //创建时间
        Date createDate = new Date();

        SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdft.format(createDate);

        //使用jdbc进行数据的持久化操作   usr
        Connection connection = JDBCUtil.getConnection();
        Statement stmt = null;
        try {
            //获取操作数据库的对象 stmt
            stmt = connection.createStatement();
            String sql = "insert into usr (username,password,birth,create_time)values('"+username+"','"+password+"','"+birth+"','"+format+"')";
            System.out.println("sql = " + sql);
            int line = stmt.executeUpdate(sql);
            //判定注册结果
            System.out.println(line>0?"成功":"失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {//释放资源
            JDBCUtil.closeAll(null, stmt, connection);
        }

    }

    //用户登录
    public static void login(){

        Scanner scanner = new Scanner(System.in);
        //获取用户名
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        //获取密码
        System.out.println("请输入密码");
        String password = scanner.nextLine();

        //使用jdbc进行数据的查询操作  username  password
        Connection connection = JDBCUtil.getConnection();
        ResultSet resultSet = null;
        Statement stmt = null;
        try {
             stmt = connection.createStatement();
            String sql = "select username,password from usr where username= '"+username+"' and password = '"+password+"' ";
            System.out.println("sql = " + sql);
            resultSet = stmt.executeQuery(sql);
            //处理结果集
            Usr usr = null;
            while (resultSet.next()){
                String uname = resultSet.getString(1);
                String pwd = resultSet.getString(2);
                 usr = Usr.builder().username(uname).password(pwd).build();
            }

           // 处理登陆的结果

            if (usr!=null) {
                System.out.println("登陆成功");
            }else {
                System.out.println("登陆失败");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {//释放资源
            JDBCUtil.closeAll(resultSet,stmt,connection);
        }

    }

}
